import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.ImageObserver;

public class Projectile extends GameObject{

	private int velocity;
	private int animationWait;
	private ProjectileControl.Owner owner;
	private final int ANIMATION_COOLDOWN = 2*InvaderSaver.GAMESPEED_MODIFIER; // change subimage every two milliseconds
	private Boolean spent;
	
	public Projectile(int x, int y, int velocity, Sprite sprite, ProjectileControl.Owner owner){
		super(x, y, sprite);
		this.velocity = velocity;
		this.owner = owner;
		
		animationWait = 0;
		image_index = 0;
		spent = false;
	}
	
	/**
	 * update the position and current sprite subimage of the projectile.
	 */
	public void update(){
		this.y_position += velocity;
		if(animationWait>=ANIMATION_COOLDOWN)
		{
			image_index++;
			animationWait = 0;
		}
		animationWait++;
		if(y_position < 0 || y_position > InvaderSaver.getGameHeight()+sprite.image_height){
			spend();
		}
	}
	
	public void spend(){
		spent = true;
		if(velocity < 0) { // then Defender is the owner
			Defender.reload();
		}
	}
	public int getVelocity(){
		return velocity;
	}
	public Boolean isSpent(){
		return spent;
	}
	public ProjectileControl.Owner getOwner(){
		return owner;
	}
}
